<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\content\controllers;

use app\libs\Pager;
use yii;
use app\libs\ApiControl;
use app\modules\content\models\Education;

class EducationController extends ApiControl
{
    public $enableCsrfValidation = false;

    /**
     * [actionIndex description]
     * @Author don
     * @Date   2021-09-27
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $data = Education::find()->asArray()->all();
        return $this->render('index', ['data' => $data]);
    }

    /**
     * [actionAdd description]
     * @Author don
     * @Date   2021-09-27
     * @return [type]     [description]
     */
    public function actionAdd()
    {
        if ($_POST) {
            $name = Yii::$app->request->post('name');
            $model = new Education();
            $model->name = $name;
            if (!$model->save()) {
                echo '<script>alert("添加失败，请重试");history.go(-1);</script>';
                die;
            } else {
                $this->redirect('index');
            }
        } else {
            return $this->render('add');
        }
    }

    /**
     * [actionUpdate description]
     * @Author don
     * @Date   2021-09-27
     * @return [type]     [description]
     */
    public function actionUpdate()
    {
        if ($_POST) {
            $name = Yii::$app->request->post('name');
            $id = Yii::$app->request->post('id');
            $model = Education::findOne($id);
            $model->name = $name;
            if (!$model->save()) {
                echo '<script>alert("修改失败，请重试");history.go(-1);</script>';
                die;
            } else {
                $this->redirect('index');
            }
        } else {
            $id = Yii::$app->request->get('id');
            $data = Education::findOne($id);
            return $this->render('update', ['data'=>$data]);
        }
    }

    /**
     * [actionDelete description]
     * @Author don
     * @Date   2021-09-27
     * @return [type]     [description]
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->get('id', '');
        $re = Education::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}